package com.example.administrator.activity.test.note;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.administrator.activity.R;

import java.util.ArrayList;

public class NoteAdapeter extends BaseAdapter {
//    上下文对象，摸个Activity
    private Context context;
//    要展示的对象
    private ArrayList<NoteBean> list;
//构造方法
    public NoteAdapeter(Context context, ArrayList<NoteBean> list) {
        this.context = context;
        this.list = list;
    }
//获取列表项的长度
    @Override
    public int getCount() {
        /**
         * 三元表达式；表达式1？表达式2：表达式3
         * 先判断表达式1：若结果为true，最终结果就是表达式
         * 若结果false，最终结果就是表达式
         */
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView==null){
//            关联布局
            convertView=View.inflate(context, R.layout.activity_note2item,null);
            viewHolder=new ViewHolder();
            viewHolder.tv_content=convertView.findViewById(R.id.note_item_content);
            viewHolder.tv_time=convertView.findViewById(R.id.note_item_time);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        viewHolder.tv_content.setText(list.get(position).getContent());
        viewHolder.tv_time.setText(list.get(position).getTime());
        return  convertView;
    }

    private class ViewHolder {
        TextView tv_content;
        TextView tv_time;
    }
}
